package com.yiwei.ydd.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.NicknameBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.constant.UserInfoGlobal;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.UI;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class NicknameSetActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_nickname)
    EditText editNickname;

    private Unbinder unbiner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nickname_set);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("昵称");
        txtRight.setText("保存");
        txtRight.setVisibility(View.VISIBLE);

    }

    public void getNickname(String nickname) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        NicknameBean item = new NicknameBean();
        item.nickname = nickname;

        loadingDialog.show();
        Api.api_service.getNickname(item).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    UserInfoGlobal.saveNickName(nickname);
                    ToastUtil.makeText(this, "修改成功");
                    finish();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.txt_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                if (TextUtils.isEmpty(UI.toString(editNickname))) {
                    ToastUtil.makeText(this, "昵称不能为空");
                    return;
                }
                if (!Util.isChinese(UI.toString(editNickname).charAt(0))) {
                    ToastUtil.makeText(this, "首位必须为中文");
                    return;
                }
                if (UI.toString(editNickname).length() < 5) {
                    ToastUtil.makeText(this, "至少要5位以上");
                    return;
                }
                getNickname(UI.toString(editNickname));
                break;
        }
    }
}
